package com.playmodule.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;

import com.playmodule.MediaUtil;
import com.playmodule.vr.VrRenderer;
import com.playmodule.vr.projections.ProjectionFactory;

import org.hitlabnz.sensor_fusion_demo.orientationProvider.OrientationProvider;

public class VrSurfaceView
        extends GLSurfaceView {
    private MediaPlayer mMediaPlayer = null;
    private boolean mPassThrough;
    private VrRenderer mRenderer;

    public VrSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public VrSurfaceView(Context context) {
        super(context);
        init(context);
    }

    public void init(Context context) {
        setEGLContextClientVersion(2);
        this.mRenderer = new VrRenderer(context, ProjectionFactory.getInstance().CreateDefault());
        setRenderer(mRenderer);
    }

    public void setMediaPlayer() {
        this.mMediaPlayer = mediaHelper.getmMediaPlayer();
        bindVideo();
    }

    private MediaUtil mediaHelper;

    public void setMediaHelper(MediaUtil mediaHelper) {
        this.mediaHelper = mediaHelper;
        mRenderer.setMediaHelper(mediaHelper);
    }

    public void bindImage(final Bitmap paramBitmap) {
        queueEvent(new Runnable() {
            public void run() {
                VrSurfaceView.this.mRenderer.bindImageTexture(paramBitmap);
            }
        });
    }

    public void bindVideo() {
        queueEvent(new Runnable() {
            public void run() {
                if (mRenderer.hasCreated) {
                    mRenderer.createVideoSurfaceTexture();
                }
                mRenderer.bindVideoTexture();
            }
        });
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (mMediaPlayer != null&&visibility!=VISIBLE) {
            mMediaPlayer.setDisplay(null);
        }
    }

    public int getMaxTextureSize() {
        int[] arrayOfInt = new int[1];
        GLES20.glGetIntegerv(3379, arrayOfInt, 0);
        return arrayOfInt[0];
    }

    public VrRenderer getRenderer() {
        return this.mRenderer;
    }

    public void onPause() {
        queueEvent(new Runnable() {
            public void run() {
                VrSurfaceView.this.mRenderer.deactivateCamera();
            }
        });
        super.onPause();
    }

    public void onResume() {
        queueEvent(new Runnable() {
            public void run() {
                VrSurfaceView.this.mRenderer.setMediaPlayer(VrSurfaceView.this.mMediaPlayer);
                VrSurfaceView.this.mRenderer.activateCamera();
                VrSurfaceView.this.mRenderer.setPassThrough(false);
            }
        });
        super.onResume();
    }

    public void setOrientationProvider(final OrientationProvider paramOrientationProvider) {
        queueEvent(new Runnable() {
            public void run() {
                VrSurfaceView.this.mRenderer.setOrientationProvider(paramOrientationProvider);
            }
        });
    }

    public void setProjection(final int paramInt1, final int paramInt2, final float paramFloat, final boolean paramBoolean) {
        queueEvent(new Runnable() {
            public void run() {
                float f = 1.33F;
                if (paramBoolean != mPassThrough) {
                    mPassThrough = paramBoolean;
                    mRenderer.setPassThrough(paramBoolean);
                }
                if (mPassThrough) {
                    mRenderer.setProjection(ProjectionFactory.getInstance().Create(0, 0, f));
                } else {
                    if (paramFloat > 0.0F) {
                        f = paramFloat;
                    }
                    mRenderer.setProjection(ProjectionFactory.getInstance().Create(paramInt1, paramInt2, f));
                }
            }
        });
    }
}
